package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"repo-master/cache"
	"repo-master/models"
	"strconv"
	"time"
)

func GitAdd(c *gin.Context) {
	git_num := c.PostForm("git_num")
	git_name := c.PostForm("git_name")
	var git = models.Git{
		GitNum:  git_num,
		GitName: git_name,
	}
	err := models.DB.Create(&git).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return

}

func OrderAdd(c *gin.Context) {
	order_Num := c.PostForm("order_Num")
	order_Code := c.PostForm("order_Code")
	gid, _ := strconv.Atoi(c.PostForm("gid"))

	var order = models.Order{
		OrderNum:  order_Num,
		OrderCode: order_Code,
		Gid:       gid,
	}
	err := models.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "成功",
	})
	return

}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	offset := (page - 1) * size
	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	var gitOrder []models.GitOrder
	/*
		SELECT
		  orders.id AS OrderID,
		  gits.git_num AS GitNum,
		  gits.git_name AS GitName,
		  orders.order_num AS OrderNum,
		  orders.order_code AS OrderCode,
			orders.gid AS Gid
		FROM
		  orders
		  LEFT JOIN gits ON orders.gid = gits.id
	*/
	redis_key := "list_id_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Joins("LEFT JOIN gits ON orders.gid = gits.id").
			Select(
				"orders.id AS OrderID",
				" gits.git_num AS GitNum",
				"gits.git_name AS GitName",
				"orders.order_num AS OrderNum",
				"orders.order_code AS OrderCode",
				"orders.gid AS Gid",
			).
			Offset(offset).
			Limit(size).
			Find(&gitOrder)
		jsonData, _ := json.Marshal(&gitOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &gitOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": gitOrder,
		"msg":  "查询成功",
	})
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var gitOrder models.GitOrder
	/*
		SELECT
		  orders.id AS OrderID,
		  gits.git_num AS GitNum,
		  gits.git_name AS GitName,
		  orders.order_num AS OrderNum,
		  orders.order_code AS OrderCode,
			orders.gid AS Gid
		FROM
		  orders
		  LEFT JOIN gits ON orders.gid = gits.id
	*/
	redis_key := "Detail_id_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Joins("LEFT JOIN gits ON orders.gid = gits.id").
			Select(
				"orders.id AS OrderID",
				" gits.git_num AS GitNum",
				"gits.git_name AS GitName",
				"orders.order_num AS OrderNum",
				"orders.order_code AS OrderCode",
				"orders.gid AS Gid",
			).
			Where("orders.id=?", id).
			Find(&gitOrder)
		jsonData, _ := json.Marshal(gitOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), gitOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": gitOrder,
		"msg":  "查询成功",
	})
}
